package com.jr.dao.impl;

import com.jr.dao.SupplierBlackListDao;
import com.jr.entry.SupplierBlackList;
import com.jr.pojo.InformationInBlackMess;
import com.jr.util.BlackPageHelper;
import com.jr.util.DBHelper;
import com.jr.util.WhitePageHelper;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

public class SupplierBlackListDaoImpl implements SupplierBlackListDao {
    DBHelper dbHelper=new DBHelper();
    @Override
    public boolean insertSupplierBlackList(SupplierBlackList supplierBlackList) {
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateString = today.format(formatter);
        String sql="insert into supplier_black_list(enterpriseId,supplierId,reason,updateDate) values(?,?,?,?)";

        return dbHelper.updateTable(sql,supplierBlackList.getEnterpriseId(),supplierBlackList.getSupplierId(),supplierBlackList.getReason(),dateString)==0? true:false ;
    }

    @Override
    public boolean deleteSupplierBlackListByEnterpriseIdAndSupplierId(String enterpriseId, String supplierId) {
        String sql="delete from supplier_black_list where enterpriseId=? and supplierId=?";
        return dbHelper.updateTable(sql,enterpriseId,supplierId)==0? true:false;
    }

    @Override
    public ArrayList<InformationInBlackMess> selectSupplierBlackListByAccountAndEnterpriseName(String account, String name) {

        StringBuilder sql=new StringBuilder("select e.`name`,u.idcardName,u.phone,e.email,b.reason,b.updateDate " +
                  "from  `user` u " +
                  "INNER JOIN  supplier_black_list b ON u.enterpriseId=b.enterpriseId " +
                  "INNER JOIN enterprise e on  e.id=b.supplierId " +
                  "where u.account=? ");
          if(name!=null) {
              sql.append("AND e.`name` LIKE ? ");
              return dbHelper.query(sql.toString(), InformationInBlackMess.class,account,"%"+name+"%");
          }
        return dbHelper.query(sql.toString(), InformationInBlackMess.class,account);
    }

    @Override
    public ArrayList<InformationInBlackMess> selectAllSupplierBlackList(String account, String name, BlackPageHelper blackPageHelper) {

        ArrayList list = new ArrayList();
        list.add(account);

         StringBuilder sql=new StringBuilder("SELECT " +
                 "    e2.name AS name, " +
                 "    u2.idcardName AS idcardName, " +
                 "    u2.phone AS phone,  " +
                 "    e2.email AS email, " +
                 "    b.reason AS reason, " +
                 "    b.updateDate  " +
                 "FROM  " +
                 "    user u  " +
                 "JOIN  " +
                 "    enterprise e1 ON u.enterpriseId = e1.id  " +
                 "JOIN  " +
                 "    supplier_black_list b ON b.enterpriseId = e1.id  " +
                 "JOIN  " +
                 "    enterprise e2 ON b.supplierId = e2.id  " +
                 "JOIN  " +
                 "    user u2 ON e2.id = u2.enterpriseId  " +
                 "WHERE  " +
                 "    u.account = ?   ");
         if (name!=null) {
             sql.append("AND e2.name LIKE ? ");
             list.add("%"+name+"%");
         }
        sql.append(" limit ?,?");
        int startIndex=(blackPageHelper.getIndexPage()-1)*blackPageHelper.getShowData();
        list.add(startIndex);
        list.add(blackPageHelper.getShowData());
          return  dbHelper.query(sql.toString(),InformationInBlackMess.class,list.toArray());
    }

    @Override
    public int selectSize(String account, String name) {
        ArrayList list = new ArrayList();
        list.add(account);

        StringBuilder sql=new StringBuilder("SELECT " +
                "    e2.name AS name, " +
                "    u2.idcardName AS idcardName, " +
                "    u2.phone AS phone,  " +
                "    e2.email AS email, " +
                "    b.reason AS reason, " +
                "    b.updateDate  " +
                "FROM  " +
                "    user u  " +
                "JOIN  " +
                "    enterprise e1 ON u.enterpriseId = e1.id  " +
                "JOIN  " +
                "    supplier_black_list b ON b.enterpriseId = e1.id  " +
                "JOIN  " +
                "    enterprise e2 ON b.supplierId = e2.id  " +
                "JOIN  " +
                "    user u2 ON e2.id = u2.enterpriseId  " +
                "WHERE  " +
                "    u.account = ?   ");
        if (name!=null) {
            sql.append("AND e2.name LIKE ? ");
            list.add("%"+name+"%");
        }
        return  dbHelper.query(sql.toString(),InformationInBlackMess.class,list.toArray()).size();
    }

}
